package com.mocoop.plugin.prompt;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.json.JSONException;
import org.json.JSONObject;

import android.view.View;

import com.mocoop.activity.R;
import com.mocoop.plugin.MocoopPlugin;
import com.mocoop.ui.MocoopFragment;
import com.mocoop.ui.PromptView;

public  class PromptUI extends MocoopPlugin {

	@Override
	public boolean execute(final String action, final CordovaArgs args, CallbackContext callbackContext) throws JSONException {

		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				MocoopFragment mocoopLayout = getActivity().getCurrentView();
				if(!mocoopLayout.getCordovaWebView().getUrl().equals(webView.getUrl())){
					return;
				}
				PromptView promptView = (PromptView) mocoopLayout.getPromptView();
				if ("showPrompt".equals(action)) {
					if (promptView == null) {
						promptView = new PromptView(getActivity());
						mocoopLayout.setPromptView(promptView);
					}
					try {
						JSONObject jsonObject = args.getJSONObject(0);
						promptView.setTypeAndClues(jsonObject.getString("type"), jsonObject.getString("text"));
					} catch (JSONException e) {
						e.printStackTrace();
					}

				} else if ("dismissPrompt".equals(action)) {
					if (promptView != null) {
						promptView.setVisibility(View.GONE);
					}
					if(mocoopLayout.getTitleStauts()!=null){
						mocoopLayout.getTitleStauts().setText(getActivity().getResources().getString(R.string.titleClues));
						mocoopLayout.hideTitleStatus();
					}
				}
			}
		});
		callbackContext.success();
		return true;
	}

}
